
local w,h = 130,10
local buffs = {
    '假死复活',
    '技能蓄力',
    '击飞',
    '晕眩',
    '蓄力中',
    '被锁定',
}
function create_life2(w,h)
    local panel = class.panel:builder
    {
        x = -800,
        y = -200,
        w = w,
        h = h,
        scale = 1,
        world_auto_remove = false,
        parent = ac.frame.portrait,
        world_anchor = 'bottom',
        normal_image = [[core\hpbar\backdrop-unit.tga]],
        buffer_rate = 1,
        updata_buff = function(self,unit)
            for _,name in ipairs(buffs) do
                local buff = unit:find_buff(name)
                if buff then
                    local title = format_obj(buff,buff.title)
                    if self.title.text~=title then
                        self.title:set_text(title)
                    end
                    if buff.time>0 then
                        local rate = 1 - buff:get_progress()
                       self.state1:set_width(self.max_w * self.scale * rate + 1)
                    else
                       self.state1:set_width(self.max_w * self.scale)
                    end
                    self.state1:show()
                    return
                end
            end
            local title = unit.ui_title or unit.title or unit:get_name()
            if self.title.text~=title then
                self.title:set_text(title)
            end
            self.state1:hide()
        end,
        updata_shield = function(self,unit)
            local hpmax = unit:get '生命上限'
            local shield = unit:get '护盾'
            local life = unit:get '生命'
            local rate = shield>0 and shield/hpmax
            if shield>0 then
                if unit.shield_update then
                    rate = unit:shield_update()
                    --self.shield:set_position(0,0)
                else
                    if shield+life>hpmax then
                        rate = math.min(rate,1)
                        self.shield:set_position(self.max_w*(1-rate) * self.scale,0)
                    else
                        self.shield:set_position(self.life.w * self.scale,0)
                    end
                end
                self.shield:set_width(self.max_w*rate * self.scale)
                self.shield:show()
            else
                self.shield:hide()
            end
        end,
        update = function(self)
            local player = ac.player.self
            local unit = self.world_widget
            local life = unit:get '生命'
            local hpmax = unit:get '生命上限'
            local rate = life/hpmax
            
            local load1 = math.max(math.abs(rate - self.buffer_rate)*0.05,0.01)
            if rate>=self.buffer_rate then
                self.buffer_rate = math.min(self.buffer_rate + load1,rate)
                self.life:set_width(self.max_w*self.buffer_rate * self.scale)
            elseif rate<self.buffer_rate then
                self.buffer_rate = math.max(self.buffer_rate - load1,rate)
                self.life:set_width(self.max_w* rate * self.scale)
            end
            if unit.hide_buffer==nil and self.buffer_rate~=rate then
                local start = math.min(rate,self.buffer_rate)
                local size = math.abs(rate - self.buffer_rate)
                self.buffer:set_position(self.max_w* start * self.scale,self.buffer.y)
                self.buffer:set_width(self.max_w* size * self.scale)
                self.buffer:show()
            else
                self.buffer:hide()
            end

            if self.updata_shield then
                self:updata_shield(unit)
            end
            if self.updata_buff then
                self:updata_buff(unit)
            end
            local image = unit:get_data('血条贴图')
            if image==nil then
                if player==unit.owner then
                    self.life:set_normal_image([[core\hpbar\绿条.tga]])
                elseif player:is_enemy(unit.owner) then
                    self.life:set_normal_image([[core\hpbar\红条.tga]])
                elseif player:is_ally(unit.owner) then
                    self.life:set_normal_image([[core\hpbar\蓝条.tga]])
                else
                    self.life:set_normal_image([[core\hpbar\黄条.tga]])
                end
            else
                self.life:set_normal_image(image)
            end
        end,
    }

    local x1 = panel.w*0.02
    local y1 = panel.h*0.15
    local w1 = panel.w*0.97
    local h1 = panel.h*0.7
    local h2 = panel.h*0.3
    panel.title = class.text:builder {
        parent = panel,
        x = -50,
        y = -62,
        w = w1+100,
        h = 60,
        color = 0xffffffff,
        shadow_offset_x = 4,
        shadow_offset_y = -4,
        shadow_color = 0xff000000,
        outline = true,
        show_flag = true,
		font_size = 14,
        align = 2,
    }
    panel.state1 = panel:add_panel([[core\hpbar\黄条.tga]],x1,-3,w1,3)
    panel.state1:hide()
    panel.life = panel:add_panel([[core\hpbar\ui_icon_05.tga]],x1,y1,w1,h1)
    panel.shield = panel:add_panel([[core\hpbar\ShieldBar.tga]],x1,y1,w1,h1)
    panel.shield:set_alpha(0.5)
    panel.shield:hide()
    panel.x1 = x1

    panel.buffer = panel.life:add_panel([[core\hpbar\ShieldBar.tga]])
    panel.buffer:set_alpha(0.5)
    panel.max_w = panel.life.w
    panel.life_h = panel.life.h

    return panel
end

local remove2 = {}
local group2 = {}
local w,h = 100,10
function unit_add_life2(unit,scale)
    local panel = unit.hp_panel or table.remove(remove2,1)
    if not panel then
        panel = create_life2(w,h)
        group2[#group2 + 1] = panel
    end
    
    panel.buffer_rate = 1
    if scale then
        panel.scale = scale
        panel:set_relative_w(1)
        panel:set_relative_size(scale)
        panel.title:set_size((scale-1)/5 + 1)
    else
        local scale = unit:get_select_scale()
        panel.scale = scale
        panel:set_relative_size(1)
        panel:set_relative_w(scale)
    end

    panel.title:set_text('')
    
    japi.SetUnitPressUIVisible(unit.handle,false)


    panel:bind_world(unit)
    unit.hp_panel = panel
    if unit.hp_trg then
        unit.hp_trg:remove()
    end
    unit.hp_trg = unit:event '单位-即将移除'(function(trg,u)
        table.insert(remove2,u.hp_panel)
    end)

    return panel
end


ac.hardware:on "界面绘制" (function ()
    for i,panel in ipairs(group2) do
        if panel:is_show() then
            panel:update()
        end
    end
end)